<script language="javascript" type="text/javascript">
      checked = false;
      function checkedAll () {
        if (checked == false){checked = true}else{checked = false}
	for (var i = 0; i < document.getElementById('ChkId').elements.length; i++) {
	  document.getElementById('ChkId').elements[i].checked = checked;
	}
      }
</script>
<script type="text/javascript">
document.getElementById('nid').value = "";
function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   //alert(KeyID)
	if(KeyID == 13)
	{
		sortDirectory();	
	}
}
function txtDir()
{
	if(document.getElementById('nid').value == "Enter Business Name") document.getElementById('nid').value = "";
}
function sortDirectory()
{
	var cat = document.getElementById('cat').value;
	var nid = document.getElementById('nid').value;
	//alert(nid);
	if(nid == "Enter Business Name") nid = "";
	var st = document.getElementById('stid').value;
	var lt = document.getElementById('ltid').value;
	window.location='index.php?tc=listing&page=<?php echo $_REQUEST[page];?>&_lt='+lt+'&_st='+st+'&cat='+cat+'&nid='+nid;
}
function show()
{
	document.sdl.action="index.php?tc=listing_add";
	document.sdl.submit();
}
function chkshow()
{
	if(confirm("Are you sure want to add categories to the selected listings?"))
	{
		document.sdl.action="index.php?tc=listing_edit&page=<?php echo $_REQUEST['page'] ?>";
		document.sdl.submit();
	}
}
</script>
<?php
if($_REQUEST['nid'] == "") $nidtxt = "Enter Business Name";
else $nidtxt =$_REQUEST['nid'];
?>
<form name="sdl" action="#" method="post" id="sd1">
<table border="0" width="100%" align="center">
<tr>
	<td align="left"><span style="color:#1B78B7; font-size:14px; font-weight:bold">Business Directory</span>&nbsp;
	<input type="button" name="addnew" value="Add New" onclick="return show();" />&nbsp;&nbsp;
    <input type="button" value="Categories" onclick="window.location='index.php?tc=cat'"  />
	</td>
	<td align="right">
    	<input type="text" name="nid" id="nid" value="<?php echo $nidtxt;?>" onkeyup="KeyCheck(event)" onfocus="txtDir()"  />
    	
    	<select name="cat" id="cat" style="width:150px" onchange="sortDirectory()" >
        	<option value="">--All Category--</option>
            <?php
			$cquery = mysql_query("select * from category order by Name");
			while($cq = mysql_fetch_array($cquery))
			{ ?>
            	<option value="<?php echo $cq['Id'];?>" <?php if($cq['Id'] == $_REQUEST['cat']) echo "selected"; ?>><?php echo $cq['Name'];?></option>
            <?php } ?>
        </select>
    
    	<select name="_lt" id="ltid" onChange="sortDirectory()">
        	<option value="" <?php if($_REQUEST['_lt'] == "") echo "selected";?>><--Sort--></option>
            <option value="1" <?php if($_REQUEST['_lt'] == 1) echo "selected";?>>Standard</option>
            <option value="2" <?php if($_REQUEST['_lt'] == 2) echo "selected";?>>Premium</option>
        </select>
    	&nbsp;
    	<select name="_st" id="stid" onChange="sortDirectory()">
        	<option value="" <?php if($_REQUEST['_st']=="") echo "selected";?>><--Sort--></option>
            <option value="2" <?php if($_REQUEST['_st']==2) echo "selected";?>>All</option>
            <option value="1" <?php if($_REQUEST['_st']==1) echo "selected";?>>Approved</option>
            <option value="3" <?php if($_REQUEST['_st']==3) echo "selected";?>>Pending</option>
        </select>	    
    </td>
</tr>
<tr>
	<td background="images/topline.gif" colspan="2" height="11"></td>
</tr>    
</table>
<?php
if($_REQUEST['_s'] == 2)
{ ?>
	<table align="center" border="0">
	<tr>
    	<td align="center"><div class="sucmsg"><img src="images/information.png" align="absmiddle" />&nbsp;Deleted Successfully</div></td>
    </tr>    
    </table>
<?php } ?>
<table width="100%" align="center" border="0" class="list">
<thead>
<tr>
	<td align="center" width="30">
	<input type="checkbox" name="allCheck" id="ChkId">
	</td>
	<td align="center" width="250">Business Name</td>
    <td align="center" width="200">Category</td>
    <td align="center" width="150">Listing Level</td>
    <td align="center" width="150">Date</td>
    <td align="center" width="150">Status</td>
    <td align="center">Action</td>
</tr>
</thead>
<?php
include('includes/function.php');
$self="index.php?tc=listing&_st=".$_REQUEST['_st']."&_lt=".$_REQUEST['_lt']."&cat=".$_REQUEST['cat']."&nid=".$_REQUEST['nid'];
$rowsPerPage = 20;
$pageNum = 1;
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
if($_GET['page']=="")
{
	$pageNum = 1;
}
/*<!--counting the offset-->*/
$offset = ($pageNum - 1) * $rowsPerPage;
$st = $_REQUEST['_st'];
$lt = $_REQUEST['_lt'];
$cat = $_REQUEST['cat'];
$nid = $_REQUEST['nid'];

if(($st == "" || $st == 2) && $lt == ""  && $cat == ""  && $nid == "")
{
	$res = mysql_query("select Id from directory");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory order by Id desc limit $offset , $rowsPerPage") or die(mysql_error());
}
else if(($st == 1 || $st == 3) && $lt == ""  && $cat == ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt != ""  && $cat == ""  && $nid == "")
{
	$res = mysql_query("select Id from directory where ListingLevel='$lt'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where ListingLevel='$lt' order by Id desc limit $offset , $rowsPerPage") or die(mysql_error());
}
else if(($st == 1 || $st == 3) && $lt != ""  && $cat == ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  ListingLevel='$lt'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and ListingLevel='$lt' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt != ""  && $cat != ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  ListingLevel='$lt' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and ListingLevel='$lt' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt != ""  && $cat == ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  ListingLevel='$lt' and BusinessName like '$nid%'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and ListingLevel='$lt' and BusinessName like '$nid%' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt != ""  && $cat != ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  ListingLevel='$lt' and BusinessName like '$nid%' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and ListingLevel='$lt' and BusinessName like '$nid%' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt == ""  && $cat != ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  BusinessName like '$nid%' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and BusinessName like '$nid%' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt == ""  && $cat == ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and  BusinessName like '$nid%'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and BusinessName like '$nid%' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == 1 || $st == 3) && $lt == ""  && $cat != ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Status='$st' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Status='$st' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt != ""  && $cat != ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where ListingLevel='$lt' and BusinessName like '$nid%' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where ListingLevel='$lt' and BusinessName like '$nid%' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt != ""  && $cat == ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where ListingLevel='$lt' and BusinessName like '$nid%'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where ListingLevel='$lt' and BusinessName like '$nid%' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt != ""  && $cat != ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where ListingLevel='$lt' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where ListingLevel='$lt' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt == ""  && $cat != ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where BusinessName like '$nid%' and Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where BusinessName like '$nid%' and Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt == ""  && $cat == ""  && $nid != "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where BusinessName like '$nid%'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where BusinessName like '$nid%' order by Id desc limit $offset , $rowsPerPage");
}
else if(($st == "" || $st == 2) && $lt == ""  && $cat != ""  && $nid == "")
{
	if($st == 3) $st=0;
	$res = mysql_query("select Id from directory where Category='$cat'");
	
	$query = mysql_query("select BusinessName, Id, Category, ListingLevel, DATE_FORMAT(InsertedDate,'%d-%m-%Y') as insdate, Status from directory where Category='$cat' order by Id desc limit $offset , $rowsPerPage");
}

$i = 1;
while($q = mysql_fetch_array($query))
{ 
	if($q["ListingLevel"] == 1) $ll = "Standard";
	if($q["ListingLevel"] == 2) $ll = "Premium";
	if($q["Status"] == 1) $status = "Approved";
	else $status = "Pending";
	?>
    <tr>
        <td align="center"><input type="checkbox" name="ChkItem[]" value="<?php echo $q['Id'];?>" onClick="checkedAll();"/></td>
        <td align="left"><?php echo $q["BusinessName"];?></td>
        <td align="left">
		<?php
		$ccg = split(",",$q['Category']);
		foreach($ccg as $cg)
		{
			$sql = mysql_query("select * from category where Id='$cg'");
			$c = mysql_fetch_array($sql);
			echo $name = $c['Name']." ";	
		}
		?>
		</td>
        <td align="center"><?php echo $ll;?></td>
        <td align="center"><?php echo $q["insdate"];?></td>
        <td align="center"><?php echo $status;?></td>
        <td align="center"><a href="index.php?tc=listing_view&_d=<?php echo $q['Id'];?>&page=<?php echo $pageNum;?>">Edit</a> | <a href="index.php?tc=listing_view&_d=<?php echo $q['Id'];?>&page=<?php echo $pageNum;?>&_a=d" onclick="return confirm('Are you sure want to delete?')">Delete</a></td>
   	</tr>     
<?php 
	$i++;
}
?>
<tr>
	<td colspan="8" style="height:33px;padding-left:5px" align="left">Add Categories to the selected listing items.</td>
</tr>
<tr>
	<td colspan="8" style="height:33px;padding-left:5px" align="left">
		<select name="itemcat">
			<option value="">-- Select Category --</option>
			<?php
				$n = mysql_query("select * from category");
				while($n1=mysql_fetch_array($n))
				{
			?>
			<option value="<?php echo $n1['Id'];?>"><?php echo $n1['Name'];?></option>
			<?php } ?>
		</select>
		&nbsp;&nbsp;
		<input type="button" name="btnsave" value="Save" onclick="return chkshow();" />
	</td>
</tr>
<?php 
if(mysql_num_rows($res) <= $rowsPerPage){	}
else { ?>
<tr>
    <td colspan="7" align="center"  bgcolor="#E3ECF3">
        <?php echo $paginglink=getPagingLink($res, $rowsPerPage,$pageNum,$self); ?>	
    </td>
</tr>
<?php } ?>
</table>
</form>